﻿<!DOCTYPE html>
<html lang="en">
<head>
    <title id='Description'>This example shows how to implement cascading comboboxes in
        jqxGrid.</title>
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxmenu.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.selection.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.pager.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxgrid.edit.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcombobox.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxpanel.js"></script>
    <script type="text/javascript" src="../../scripts/demos.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var data = '[{ "Country": "Belgium", "City": "Brussels"}, {"Country": "France", "City": "Paris"}, {"Country": "USA", "City": "Washington"}]';
            // prepare the data
            var source =
            {
                datatype: "json",
                datafields: [
                    { name: 'Country', type: 'string' },
                    { name: 'City', type: 'string' }
                ],
                localdata: data
            };
            var dataAdapter = new $.jqx.dataAdapter(source);

            $("#jqxgrid").jqxGrid(
            {
                width: 300,
                source: dataAdapter,
                selectionmode: 'singlecell',
                editable: true,
                autoheight: true,
                columns: [
                    {
                        text: 'Country', datafield: 'Country', width: 150, columntype: 'combobox',
                        cellvaluechanging: function (row, datafield, columntype, oldvalue, newvalue) {
                            if (newvalue != oldvalue) {
                                $("#jqxgrid").jqxGrid('setcellvalue', row, "City", "Select a city...");
                            };
                        }
                    },
                    {
                        text: 'City', datafield: 'City', width: 150, columntype: 'combobox',
                        initeditor: function (row, cellvalue, editor, celltext, cellwidth, cellheight) {
                            var country = $('#jqxgrid').jqxGrid('getcellvalue', row, "Country");
                            var city = editor.val();

                            var cities = new Array();
                            switch (country) {
                                case "Belgium":
                                    cities = ["Bruges", "Brussels", "Ghent"];
                                    break;
                                case "France":
                                    cities = ["Bordeaux", "Lille", "Paris"];
                                    break;
                                case "USA":
                                    cities = ["Los Angeles", "Minneapolis", "Washington"];
                                    break;
                            };

                            editor.jqxComboBox({ autoDropDownHeight: true, source: cities });
                            if (city != "Select a city...") {
                                var index = cities.indexOf(city);
                                editor.jqxComboBox('selectIndex', index);
                            }
                        }
                    }
                ]
            });
        });
    </script>
</head>
<body class='default'>
    <div id="jqxgrid">
    </div>
</body>
</html>